# Prepare stage for multistage image build
## START OF STAGE0 ##
FROM swr.cn-north-4.myhuaweicloud.com/eg-common/openjdk:8u201-jre-alpine

# Define all environment variable here
ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk
# # CREATE APP USER ##
# Set umask
RUN sed -i "s|umask 022|umask 027|g" /etc/profile

# Create the home directory for the new app user.
RUN mkdir -p /usr/app
RUN mkdir -p /usr/app/bin

# Set the home directory to our app user's home.
ENV APP_HOME=/usr/app
ENV UID=166
ENV GID=166
ENV USER_NAME=eguser
ENV GROUP_NAME=eggroup
ENV ENV="/etc/profile"

# Create an app user so our program doesn't run as root.
RUN apk update &&\
    apk add shadow &&\
    groupadd -r -g $GID eggroup &&\
    useradd -r -u $UID -g $GID -d $APP_HOME -s /sbin/nologin -c "Docker image user" eguser

RUN groupadd -g 1166 docker &&\
    gpasswd -a $USER_NAME docker
# Set the working directory.
WORKDIR $APP_HOME

RUN chmod -R 750 $APP_HOME &&\
    chmod -R 550 $APP_HOME/bin &&\
    mkdir -p -m 750 $APP_HOME/config &&\
    mkdir -p -m 750 $APP_HOME/log &&\
    mkdir -p -m 750 $APP_HOME/packages &&\
    mkdir -p -m 700 $APP_HOME/ssl &&\
    mkdir -p -m 700 $APP_HOME/.postgresql &&\
    chown -R eguser:eggroup $APP_HOME

# Copy the application & scripts
COPY --chown=eguser:eggroup target/apm-0.0.1-SNAPSHOT.jar $APP_HOME/bin
COPY --chown=eguser:eggroup configs/*.sh $APP_HOME/bin

# Exposed port
EXPOSE 8064

# Change to the app user.
#USER eguser

# Execute script & application
ENTRYPOINT ["sh", "./bin/start.sh"]
